package com.one.common.manager.imageloader;

import android.content.Context;
import android.graphics.Color;
import android.net.Uri;
import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;

import java.io.File;

/**
 * @Description: 使用Glide框架加载图片
 * @author: <a href="http://www.xiaoyaoyou1212.com">DAWI</a>
 * @date: 2016-12-19 15:16
 */
public class GlideLoader implements ILoader {
    @Override
    public void init(Context context) {
        try {
            Class.forName("com.bumptech.glide.Glide");
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("Must be dependencies Glide!");
        }
    }

    @Override
    public void loadNet(ImageView target, String url) {
        load(getRequestManager(target.getContext()).load(url), target, null);
    }

    @Override
    public void loadNet(ImageView target, String url, Options options) {
        load(getRequestManager(target.getContext()).load(url), target, options);
    }

    @Override
    public void loadResource(ImageView target, int resId, Options options) {
        load(getRequestManager(target.getContext()).load(resId), target, options);
    }

    @Override
    public void loadAssets(ImageView target, String assetName, Options options) {
        load(getRequestManager(target.getContext()).load("file:///android_asset/" + assetName), target, options);
    }

    @Override
    public void loadFile(ImageView target, File file, Options options) {
        load(getRequestManager(target.getContext()).load(file), target, options);
    }

    @Override
    public void loadUri(ImageView target, Uri uri, Options options) {
        load(getRequestManager(target.getContext()).load(uri), target, options);
    }

    @Override
    public void clearMemoryCache(Context context) {
        Glide.get(context).clearMemory();
    }

    @Override
    public void clearDiskCache(Context context) {
        Glide.get(context).clearDiskCache();
    }

    private RequestManager getRequestManager(Context context) {
        return Glide.with(context);
    }

    private void load(DrawableTypeRequest request, ImageView target, Options options) {
        if (options == null){
            options = Options.defaultOptions();
//            options.setCenterCrop(true);
            request.fitCenter();
        }

        if (options.loadingResId != Options.RES_NONE) {
            request.placeholder(options.loadingResId);
        }
        if (options.loadErrorResId != Options.RES_NONE) {
            request.error(options.loadErrorResId);
        }
        if (options.WIDTH != Options.RES_NONE && options.HEIGHT != Options.RES_NONE) {
            request.override(options.WIDTH, options.HEIGHT);
        }
        // 圆形
        if (options.isCircle) {
            request.transform(new GlideCircleTransform(target.getContext()));
        }
        if (options.isCenterCrop()) {
            request.centerCrop();
        }

        // 遮罩
        if(options.isMask){
            request.transform(new GlideMaskTransform(target.getContext()));
        }

        // 高斯模糊
        if(options.isBlur()){
            request.transform(new BlurTransformation(target.getContext(),
                    4, Color.parseColor("#4b000000")));
        }

        // 圆角
        if (options.getRound() > 0) {
            request.transform(new GlideRoundTransform(target.getContext(), options.getRound()));
        }

        request.crossFade().into(target);
    }
}
